<?php
	defined('IN_PHPCMS') or exit('No permission resources.');
	pc_base::load_plugin_class('weibooauth','',0);
	class plugin_admin {
		
		function __construct($pluginid) {
			$this->pluginid = $pluginid;
			$this->db = pc_base::load_model('content_model');
			$this->op = pc_base::load_app_class('plugin_op');
			$this->auth_db = pc_base::load_plugin_model('weibo_auth_info_model');
		}
		
		public function oauth() {			
			$setting = getcache('weibo_var','plugins');			
			if(!isset($_REQUEST['oauth_verifier']) || $_REQUEST['oauth_verifier'] == '') {
				if($this->auth_db->get_one(array('source'=>'sina'))) {
					$txt = '授权成功，<a href="?m=admin&c=plugin&a=config&pluginid='.$this->pluginid.'&module=removeauth&pc_hash='.$_SESSION['pc_hash'].'">解除绑定</a>';
				} else {
					$o = new WeiboOAuth( $setting['wb_akey'] , $setting['wb_skey']);
					$keys = $o->getRequestToken();
					$aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , get_url());
					$_SESSION['keys'] = $keys;
					$txt = '<a href="javascript:;" onclick="opensinaoauth(\''.$aurl.'\')">点击进行授权</a>';
				}
				
			} else {
				$o = new WeiboOAuth( $setting['wb_akey'] , $setting['wb_skey'] , $_SESSION['keys']['oauth_token'] , $_SESSION['keys']['oauth_token_secret']  );
				$last_key = $o->getAccessToken(  $_REQUEST['oauth_verifier'] ) ;				
				$c = new WeiboClient( $setting['wb_akey'] , $setting['wb_skey'] , $last_key['oauth_token'] , $last_key['oauth_token_secret']  );
				$ms  = $c->home_timeline(); // done
				$me = $c->verify_credentials();	
				$this->auth_db->insert(array('uid'=>$me['id'],'token'=>$last_key['oauth_token'],'tsecret'=>$last_key['oauth_token_secret'],'source'=>'sina'));
				$url = '?m=admin&c=plugin&a=config&pluginid='.$this->pluginid.'&module=removeauth&pc_hash='.$_SESSION['pc_hash'];
				$tips = $me['name'].'您好，您的授权已经成功,请关闭该窗口后刷新页面';
                $oauth = 1;
			}
			include $this->op->plugin_tpl('oauth',PLUGIN_ID);
		}

		public function removeauth() {
			if($this->auth_db->delete(array('source'=>'sina'))) {
				showmessage('解除绑定成功');
			} else {
				showmessage('解除绑定失败');
			}
		}
		public function pushtoweibo() {
			$pluginid = $this->pluginid;
			if(isset($_POST[sendsubmit])) {
				$c = $this->weiboclient();
				$me = array_iconv($c->verify_credentials(),'utf-8',CHARSET);			
				$text = $_POST['title'].' '.$_POST[url];
				if($_POST[thumb]) {
					$a = $c->upload($text,$_POST[thumb]);
				} else {
					$a = $c->update($text);
				}
				$callback_msg = array_iconv($a,'utf-8',CHARSET);				
				if(!isset($callback_msg['error_code'])) exit('1');
				else exit('0');
			} else {
				$modelid = intval($_GET['modelid']);
				$id_arr = explode('|',$_GET['id']);
				$this->db->set_model($modelid);
				foreach($id_arr as $id) {
					$result[] = $this->db->get_one(array('id'=>intval($id)));
					//$text = $r['title'].' '.$r[url].' @'.$me['name'];
					
					/*
					echo $r[thumb];
					if($r[thumb]) {
						$a = $c->upload(iconv(CHARSET,'utf-8',$text),$r[thumb]);
					} else {
						$a = $c->update(iconv(CHARSET,'utf-8',$text));
					}
					$callback_msg = array_iconv($a,'utf-8',CHARSET);
					if(!isset($callback_msg['error_code'])) echo $r['title'].'发送成功!'.'<br>';
					else echo $r['title'].'发送失败!'.'错误代码:'.$callback_msg['error_code'].' 出错信息:'.$callback_msg['error'].'<br>';
					*/
				}
				include $this->op->plugin_tpl('push_list',PLUGIN_ID);
			}
		}
		
		private function weiboclient() {
			$setting = getcache('weibo_var','plugins');	
			$token_data = $this->auth_db->get_one(array('source'=>'sina'));
			$c = new WeiboClient( $setting['wb_akey'] , $setting['wb_skey'] , $token_data['token'] , $token_data['tsecret'] );
			return $c;
		}
	}
?>